const express = require('express')
const AutoDir = require('require-directory')
const cors = require('cors')
var cookieParser = require('cookie-parser')
var path = require('path')
function initManage(app) {
  app.use(express.json())
  app.use(express.urlencoded({ extended: false }))
  app.use(cookieParser()) // ?
  app.use(express.static(path.join(__dirname, 'public')))
  /* 1. 导入第三方中间件 */
  app.use(cors()) // 挂载跨域请求中间件
  /* 2. 路由模块的自动导入 */
  const useRoutes = (obj) => {
    /* obj.stack为真就是一个路由 */
    if (obj.stack) {
      app.use(obj)
    }
  }
  AutoDir(module, '../router', { visit: useRoutes })
}
module.exports = initManage
